function lstinf$(a$,b$,c$)
  adra=1:adrb=1:blngt=len(b$):d$=""
  while adrb<=blngt
    if mid$ (a$,adra,1)=mid$ (b$,adrb,1) then:adrb+=1:end if
    adra=adra+1
    end while
  while mid$(a$,adra,1)<>left$(c$,1)
    d$=d$+mid$(a$,adra,1):adra=adra+1
    end while
  lstinf$=d$
  end function

function vldc(a$)
  t0$="!"+a$+".0.0.0."
  t1$=lstinf$(t0$,"!",".")
  t2$=lstinf$(t0$,".",".")
  t3=10^(len(t2$))
  vldc=val(t1$)+(val(t2$)/t3)
  end function

dim chfq[16],chvl[16]

finp$="_.csv"
if argc>2 then:finp$=argv(2):end if

print"56676D20 ;- vgm"
print"FFFFFF0F ;- eof"
print"60010000 ;- version"
print"00000000 ;- 999E3600 ;- sn clock ;- tirar"
print"00000000 ;- "
print"0A080000 ;- gd3"
print"30130800 ;- total samples"
print"67000000 ;- loop offset"
print"00000000 ;- "
print"3C000000 ;- rate"
print"09001000 ;- flag bits"
print"B60A7540 ;- ym2612 clock"
print"00000000 ;- "
print"4C000000 ;- vgm data offset"
print"00000000000000000000000000000000"
print"00000000000000000000000000000000"
print"00000000000000000000000000000000"
print"00000000000000000000000000000000"
print"0000000000000000"
print

print"522208 522700"
print"52B4C0 52B5C0 52B6C0"
print"53B4C0 53B5C0 53B6C0"
print"522800 522801 522802 522803 522804 522805 522806"
print"523131 524119 52511F 526100 527100 528100 529100"
print"523502 52450F 52551F 526500 527500 528506 529500"
print"523975 52490F 52591F 526900 527900 528906 529900"
print"523D08 524D0F 525D1F 526D00 527D00 528D06 529D00"
print"52B136 52A51A 52A1D3 5228F1 "
print"523231 524219 52521F 526200 527200 528200 529200"
print"523602 52460F 52561F 526600 527600 528606 529600"
print"523A75 524A0F 525A1F 526A00 527A00 528A06 529A00"
print"523E08 524E0F 525E1F 526E00 527E00 528E06 529E00"
print"52B236 52A61B 52A22B 5228F2"
print"533031 534019 53501F 536000 537000 538000 539000"
print"533402 53440F 53541F 536400 537400 538406 539400"
print"533875 53480F 53581F 536800 537800 538806 539800"
print"533C08 534C0F 535C1F 536C00 537C00 538C06 539C00"
print"53B036 53A41B 53A05B 5228F4"
print"533131 534119 53511F 536100 537100 538100 539100"
print"533502 53450F 53551F 536500 537500 538506 539500"
print"533975 53490F 53591F 536900 537900 538906 539900"
print"533D08 534D0F 535D1F 536D00 537D00 538D06 539D00"
print"53B136 53A51B 53A1C5 5228F5 "
print"533231 534219 53521F 536200 537200 538200 539200 "
print"533602 53460F 53561F 536600 537600 538606 539600"
print"533A75 534A0F 535A1F 536A00 537A00 538A06 539A00"
print"533E08 534E0F 535E1F 536E00 537E00 538E06 539E00"
print"53B236 53A61C 53A23B 5228F6 61D089"
print"523031 524019 52501F 526000 527000 528000 529000"
print"523402 52440F 52541F 526400 527400 528406 529400"
print"523875 52480F 52581F 526800 527800 528806 529800"
print"523C08 524C0F 525C1F 526C00 527C00 528C06 529C00 "
print"52B036 52A41A 52A084"
print"5228F0 522801 522802 522804 522805 522806"

print"A22208 A22700"
print"A2B4C0 A2B5C0 A2B6C0"
print"A3B4C0 A3B5C0 A3B6C0"
print"A22800 A22801 A22802 A22803 A22804 A22805 A22806"
print"A23131 A24119 A2511F A26100 A27100 A28100 A29100"
print"A23502 A2450F A2551F A26500 A27500 A28506 A29500"
print"A23975 A2490F A2591F A26900 A27900 A28906 A29900"
print"A23D08 A24D0F A25D1F A26D00 A27D00 A28D06 A29D00"
print"A2B136 A2A51A A2A1D3 A228F1 "
print"A23231 A24219 A2521F A26200 A27200 A28200 A29200"
print"A23602 A2460F A2561F A26600 A27600 A28606 A29600"
print"A23A75 A24A0F A25A1F A26A00 A27A00 A28A06 A29A00"
print"A23E08 A24E0F A25E1F A26E00 A27E00 A28E06 A29E00"
print"A2B236 A2A61B A2A22B A228F2"
print"A33031 A34019 A3501F A36000 A37000 A38000 A39000"
print"A33402 A3440F A3541F A36400 A37400 A38406 A39400"
print"A33875 A3480F A3581F A36800 A37800 A38806 A39800"
print"A33C08 A34C0F A35C1F A36C00 A37C00 A38C06 A39C00"
print"A3B036 A3A41B A3A05B A228F4"
print"A33131 A34119 A3511F A36100 A37100 A38100 A39100"
print"A33502 A3450F A3551F A36500 A37500 A38506 A39500"
print"A33975 A3490F A3591F A36900 A37900 A38906 A39900"
print"A33D08 A34D0F A35D1F A36D00 A37D00 A38D06 A39D00"
print"A3B136 A3A51B A3A1C5 A228F5 "
print"A33231 A34219 A3521F A36200 A37200 A38200 A39200 "
print"A33602 A3460F A3561F A36600 A37600 A38606 A39600"
print"A33A75 A34A0F A35A1F A36A00 A37A00 A38A06 A39A00"
print"A33E08 A34E0F A35E1F A36E00 A37E00 A38E06 A39E00"
print"A3B236 A3A61C A3A23B A228F6 61D089"
print"A23031 A24019 A2501F A26000 A27000 A28000 A29000"
print"A23402 A2440F A2541F A26400 A27400 A28406 A29400"
print"A23875 A2480F A2581F A26800 A27800 A28806 A29800"
print"A23C08 A24C0F A25C1F A26C00 A27C00 A28C06 A29C00 "
print"A2B036 A2A41A A2A084"
print"A228F0 A22801 A22802 A22804 A22805 A22806"

lct=0

open finp$ for input as #1
while eof(1)=0
  file input #1,txrd$
  setcaption("b:"+str$(lct)):lct+=1
  if mid$(txrd$,1,1)="[" then:
    if mid$(txrd$,2,1)="f" then:
      chid=val(mid$(txrd$,3,2))
      chid=chid mod 12 'ym2612x2 has 12 channels
      chfqv=vldc(lstinf$(txrd$,":","]"))
      chfqvo=int(chfqv)
      if chfqvo<1 then:chfqvo=1:end if
      if chfqvo>8 then:chfqvo=8:end if
      chfqvf=chfqv-chfqvo
      chfqv3=int(644*2^chfqvf)
      chfqv3a=chfqv3 mod 256
      chfqv3b=int(chfqv3/256)
      chfqv5=(chfqvo*8+chfqv3b)
      chfqv2=int(26.6984611843*2^(8-chfqv))
      if chfqv2>2047 then:chfqv2=2047:end if
      if chfqv2<0 then:chfqv2=0:end if
      chchrfq$=right$("000"+hex$(chfqv2),3)
      if int(chid/6)=0 then:
        txrgo$="52280"+right$("0"+hex$((chid mod 6)),1)
        txrgo$+=" 52A4"+right$("00"+hex$(chfqv5),2)
        txrgo$+=" 52A0"+right$("00"+hex$(chfqv3a),2)
        txrgo$+=" 5228F"+right$("0"+hex$((chid mod 6)),1)
      else
        txrgo$="A2280"+right$("0"+hex$(  (chid mod 6) ),1)
        txrgo$+=" A2A4"+right$("00"+hex$(chfqv5),2)
        txrgo$+=" A2A0"+right$("00"+hex$(chfqv3a),2)
        txrgo$+=" A228F"+right$("0"+hex$((chid mod 6)),1)
        end if
      print ucase$(txrgo$)
      end if
    'if mid$(txrd$,2,1)="v" then:
    '  chid=val(mid$(txrd$,3,2))
    '  chid=chid mod 12 'ym2612x2 has 12 channels
    '  chvlv=vldc(lstinf$(txrd$,":","]"))*0x10
    '  chchrvl$=right$("00"+hex$(chvlv),2)
    '  txrgo$="A0"+right$("00"+hex$(8+chid),2)+chchrvl$
    '  print ucase$(txrgo$)
    '  end if
    if mid$(txrd$,2,3)="wvb" then:
      wvblv=vldc(lstinf$(txrd$,":","]"))
      wvblv2=wvblv*44100
      wvblv3a=wvblv2 mod 65536
      wvblv3b=int(wvblv2/65536)
      wvblv3as$=ucase$(right$("0000"+hex$(wvblv3a),4))
      for ii=1 to wvblv3b:print"61FFFF":next
      print "61"+mid$(wvblv3as$,3,2)+mid$(wvblv3as$,1,2)
      end if
    end if
  wend
print:print "66":print
close #1


